﻿/*----------------------------------------------------------------
    // Copyright (C) 2009 江苏华招网
    // 版权所有。 
    // 作者：宣帅
    //
    // 文件名：OrderDistributionSend.cs
    // 文件功能描述：配送查询 
    // 创建标识：宣帅2009-05-13
    //
    // 修改标识：
    // 修改描述：
    //
    // 修改标识：
    // 修改描述：
    //
    // 审核人：
    // 审核时间：
    // 审核说明：
 //----------------------------------------------------------------*/

using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using HZ.DrugTrade.Web;
using HZ.DrugTrade.Model;
using HZ.DrugTrade.IBLL;
using HZ.DrugTrade.BLLFactory;
using HZ.DrugTrade.Model.Enum;

namespace HZ.DrugTrade.Web.Enterprise.OrderManage
{
    public partial class OrderDistributeSend : BasePage
    {
        public OrderDistributeSend()
        {
            this.ModuleNo = Module.配送单管理.ToString();
        }

        IBLL_Purchase iPurchase = Factory.BLL_Purchase();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Bind();
            }
        }

        protected void lbnSwitch_Click(object sender, EventArgs e)
        {
            if (lbnSwitch.CommandArgument == "simple")
            {
                ltlAdvance.Text = "";
                lbnSwitch.CommandArgument = "advance";
                lbnSwitch.Text = "简单查询";
            }
            else
            {
                ltlAdvance.Text = "<tbody id='advance' style='display:none'>";
                lbnSwitch.CommandArgument = "simple";
                lbnSwitch.Text = "高级查询";

                //清空数据 

                txtOutLookc.Text = "";
                txtStartDate.Text = "";
                txtEndDate.Text = "";
                txtHospitalName.Text = "";
                txtpinyincode.Text = "";
                txtBatchRecordID.Text = "";
                txtGoodsName.Text = "";
            }
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            AspNetPager1.CurrentPageIndex = 1;
            Bind();
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            Bind();
        }

        public void Bind()
        {
            HZ.DrugTrade.Model.Query.Distribution QueryDistribution = new HZ.DrugTrade.Model.Query.Distribution();

            QueryDistribution.DistributeTimeFrom = new HZ.Common.ConvertHelper(txtStartDate.Text.Trim()).ToDateTime;
            QueryDistribution.DistributeTimeTo = new HZ.Common.ConvertHelper(txtEndDate.Text.Trim()).ToDateTime;
            QueryDistribution.BatchRecordID = txtBatchRecordID.Text.Trim();
            QueryDistribution.HospitalName = this.txtHospitalName.Text.Trim();
            QueryDistribution.Outlookc = this.txtOutLookc.Text.Trim();
            QueryDistribution.PinyinCode = this.txtpinyincode.Text.Trim().ToUpper();
            QueryDistribution.CompanyID_PS = CurrentAccount.GetUser().Org.OrgID;
            QueryDistribution.GoodsName = this.txtGoodsName.Text.Trim();
            QueryDistribution.ProjectID = this.ddlProjectid.SelectedValue;
            QueryDistribution.Material = this.txtMaterial.Text.Trim();
            QueryDistribution.DistributeState = 0;
            QueryDistribution.OrderDetailState = 2;
            QueryDistribution.SortFields = "ExecuteTime desc ,OrderName ASC,HospitalName ASC,GoodsName ASC,BatchRecordID ASC ";
            //AspNetPager1.PageSize = Constants.GRIDVIEW_PAGE_SIZE;
            int totalrecord = 0;
            try
            {
                IList<HZ.DrugTrade.Model.View.Distribution> vDistributionList = iPurchase.GetPurchaseOrderDetail_DistributeByWhere(QueryDistribution, this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageSize, out totalrecord);
                gvwProduceCatalog.AutoGenerateColumns = false;
                gvwProduceCatalog.DataSource = vDistributionList;
                gvwProduceCatalog.DataBind();
                AspNetPager1.RecordCount = totalrecord;
            }
            catch (Exception ex)
            {
                PageError(ex.Message, "");
            }


            if (gvwProduceCatalog.Rows.Count <= 0)
            {
                btnExport.Visible = false;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvwProduceCatalog_RowEditing(object sender, GridViewEditEventArgs e)
        {
            this.gvwProduceCatalog.EditIndex = e.NewEditIndex;
            this.Bind();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvwProduceCatalog_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            if (Page.IsValid)
            {
                int rowIndex = e.RowIndex;

                string OrderDetailID = this.gvwProduceCatalog.DataKeys[rowIndex].Value.ToString();
                string SendNum = ((TextBox)this.gvwProduceCatalog.Rows[rowIndex].FindControl("txtDis_DisTributeCount")).Text.Trim();//修改后的退货量
                string SoldNum = ((Label)gvwProduceCatalog.Rows[rowIndex].FindControl("Ltruen")).Text.Trim();//页面加载是的退货量   
                //todo:是否需要在这边判断配送数量大于采购数量 

                int? num = new HZ.Common.ConvertHelper(SendNum).ToInt32;
                int? oldnum = new HZ.Common.ConvertHelper(SoldNum).ToInt32;
                if (num == null || num == 0)
                {
                    HZ.Web.WebUtil.Alert("配送数量填写有误！", this);
                    ((TextBox)gvwProduceCatalog.Rows[rowIndex].FindControl("txtDis_DisTributeCount")).Focus();
                    return;
                }         //虚拟的退货总量-加载的数量+填入的退货数量 不能大于 入库数量
                else if (new HZ.Common.ConvertHelper(gvwProduceCatalog.Rows[rowIndex].Cells[6].Text.Trim()).ToInt32 - oldnum + num > new HZ.Common.ConvertHelper(gvwProduceCatalog.Rows[rowIndex].Cells[5].Text.Trim()).ToInt32)
                {
                    HZ.Web.WebUtil.Alert("配送数量之和大于采购数量！", this);
                    ((TextBox)gvwProduceCatalog.Rows[rowIndex].FindControl("txtDis_DisTributeCount")).Focus();
                    return;
                }
                else
                {
                    HZ.DrugTrade.Model.Distribution aDistribution = new HZ.DrugTrade.Model.Distribution();
                    HZ.DrugTrade.Model.DistributionBatchRecord aDistributionBatchRecord = new HZ.DrugTrade.Model.DistributionBatchRecord();
                    List<HZ.DrugTrade.Model.Distribution> DistributionList = new List<Distribution>();
                    List<HZ.DrugTrade.Model.DistributionBatchRecord> DistributionBatchRecordList = new List<DistributionBatchRecord>();
                    aDistribution.DistributeCount = num;
                    aDistribution.OrderDetailID = gvwProduceCatalog.DataKeys[e.RowIndex].Values[1].ToString();
                    aDistribution.DistributionSerialID = gvwProduceCatalog.DataKeys[e.RowIndex].Values[0].ToString();

                    aDistributionBatchRecord.DistributionSerialID = gvwProduceCatalog.DataKeys[e.RowIndex].Values[0].ToString();
                    aDistributionBatchRecord.BatchRecordID = new HZ.Common.ConvertHelper(((TextBox)this.gvwProduceCatalog.Rows[rowIndex].FindControl("txtBatchRecordID")).Text.Trim()).String;
                    DistributionList.Add(aDistribution);
                    DistributionBatchRecordList.Add(aDistributionBatchRecord);
                    int state = iPurchase.EditPurchaseOrderDetail_Distribution_Batch(DistributionList, DistributionBatchRecordList);
                    if (state > 0)
                    {
                        gvwProduceCatalog.EditIndex = -1;
                        this.Bind();
                    }
                    else if (state == 2)
                    {
                        HZ.Web.WebUtil.Alert("该产品医疗机构已入库！", this);
                        this.Bind();
                    }
                    else
                    {
                        //PageError("更新失败！", "");
                        HZ.Web.WebUtil.Alert("更新失败", this);
                    }
                }
            }
        }

        protected void gvwProduceCatalog_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            this.gvwProduceCatalog.EditIndex = -1;
            this.Bind();
        }

        protected void gvwProduceCatalog_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

            HZ.DrugTrade.Model.Distribution aDistribution = new HZ.DrugTrade.Model.Distribution();
            HZ.DrugTrade.Model.DistributionBatchRecord aDistributionBatchRecord = new HZ.DrugTrade.Model.DistributionBatchRecord();
            List<HZ.DrugTrade.Model.Distribution> DistributionList = new List<Distribution>();
            List<HZ.DrugTrade.Model.DistributionBatchRecord> DistributionBatchRecordList = new List<DistributionBatchRecord>();

            aDistribution.OrderDetailID = gvwProduceCatalog.DataKeys[e.RowIndex].Values[1].ToString();
            aDistribution.DistributionSerialID = gvwProduceCatalog.DataKeys[e.RowIndex].Values[0].ToString();

            aDistributionBatchRecord.DistributionSerialID = gvwProduceCatalog.DataKeys[e.RowIndex].Values[0].ToString();
            //aDistributionBatchRecord.BatchRecordID = new HZ.Common.ConvertHelper(((TextBox)this.gvwProduceCatalog.Rows[e.RowIndex].FindControl("txtBatchRecordID")).Text.Trim()).String;                                 //updated by lhl 2009-08-31
            aDistributionBatchRecord.BatchRecordID = new HZ.Common.ConvertHelper(((Label)this.gvwProduceCatalog.Rows[e.RowIndex].FindControl("Label1")).Text.Trim()).String;
            DistributionList.Add(aDistribution);
            DistributionBatchRecordList.Add(aDistributionBatchRecord);
            if (iPurchase.DeletePurchaseOrderDetail_Distribution_Batch(DistributionList) > 0)
            {
                gvwProduceCatalog.EditIndex = -1;
                this.Bind();
            }
            else
            {
                //PageError("删除失败！", "");
                HZ.Web.WebUtil.Alert("删除失败！", this);
            }

        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            HZ.DrugTrade.Model.Query.Distribution QueryDistribution = new HZ.DrugTrade.Model.Query.Distribution();

            QueryDistribution.DistributeTimeFrom = new HZ.Common.ConvertHelper(txtStartDate.Text.Trim()).ToDateTime;
            QueryDistribution.DistributeTimeTo = new HZ.Common.ConvertHelper(txtEndDate.Text.Trim()).ToDateTime;
            QueryDistribution.ProjectID = this.ddlProjectid.SelectedValue;
            QueryDistribution.BatchRecordID = txtBatchRecordID.Text.Trim();
            QueryDistribution.HospitalName = this.txtHospitalName.Text.Trim();
            //QueryDistribution.PinyinCode = this.txtPinyinCode.Text.Trim();
            QueryDistribution.GoodsName = this.txtGoodsName.Text.Trim();
            QueryDistribution.Outlookc = this.txtOutLookc.Text.Trim();
            QueryDistribution.Material = this.txtMaterial.Text.Trim();
            QueryDistribution.CompanyID_PS = CurrentAccount.GetUser().Org.OrgID;
            QueryDistribution.PinyinCode = this.txtpinyincode.Text.Trim().ToUpper();
            QueryDistribution.DistributeState = 0;
            QueryDistribution.OrderDetailState = 2;
            QueryDistribution.SortFields = "ExecuteTime desc ,OrderName ASC,HospitalName ASC,GoodsName ASC,BatchRecordID ASC ";

            int totalrecord = 0;
            IList<HZ.DrugTrade.Model.View.Distribution> vDistributionList = iPurchase.GetPurchaseOrderDetail_DistributeByWhere(QueryDistribution, 1, 0, out totalrecord);

            DataTable dt = HZ.Data.ModelConvert.GetDataTableFromModel(vDistributionList);

            HZ.Excel.ExcelHelper.ExportDataForWeb(dt, gvwProduceCatalog);
        }

        protected void gvwProduceCatalog_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                if (ddlProjectid.SelectedItem.Value == "0002")
                {
                    e.Row.Cells[3].Text = "自主报价";
                }
                else
                {
                    e.Row.Cells[3].Text = "采购限价";
                }
            }
        }
    }
}
